package aeropuertos.intentos;

import java.util.Calendar;

import aeropuertos.intent.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

@SuppressWarnings("unused")
public class Pantalla_Add extends Activity {
	static final int DATE_DIALOG_ID = 0;
	static final int ERROR_DIALOG = 1 ;
	private EditText code; 
	private EditText country;

	 private TextView mDateDisplay;
	    private Button mPickDate;

	    private int mYear;
	    private int mMonth;
	    private int mDay;
	 

 	public static final int INSERT_ID = Menu.FIRST;
	@Override
	public void onCreate(Bundle savedInstanceState) {
	 super.onCreate(savedInstanceState);

     setContentView(R.layout.add_pantalla);
     

     setTitle(R.string.Add);
     Log.v("pantallaAdd", "inicia2"); //v para verbose 
     code = (EditText) findViewById(R.id.editText1);
     country = (EditText) findViewById(R.id.editText2);
     mDateDisplay = (TextView) findViewById(R.id.fecha);
     mPickDate = (Button) findViewById(R.id.buttonPicker);
    
     // add a click listener to the button
     mPickDate.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             showDialog(DATE_DIALOG_ID);
         }
     });
     
     final Calendar c = Calendar.getInstance();
     mYear = c.get(Calendar.YEAR);
     mMonth = c.get(Calendar.MONTH);
     mDay = c.get(Calendar.DAY_OF_MONTH);
Log.v("Pantalla_add","antes update display");
     // display the current date
     updateDisplay();
     Log.v("Pantalla_add","despues update display");

	}
	  private void updateDisplay() {
		// TODO Auto-generated method stub
		  mDateDisplay.setText(
		            new StringBuilder()
		                    // Month is 0 based so add 1
		                    .append(mMonth + 1).append("-")
		                    .append(mDay).append("-")
		                    .append(mYear).append(" "));
		
	}
	  private DatePickerDialog.OnDateSetListener mDateSetListener =
          new DatePickerDialog.OnDateSetListener() {

              public void onDateSet(DatePicker view, int year, 
                                    int monthOfYear, int dayOfMonth) {
                  mYear = year;
                  mMonth = monthOfYear;
                  mDay = dayOfMonth;
                  updateDisplay();
              }
          };
          @Override
          protected Dialog onCreateDialog(int id) {
              switch (id) {
              case DATE_DIALOG_ID:
                  return new DatePickerDialog(this,
                              mDateSetListener,
                              mYear, mMonth, mDay);
              
              case ERROR_DIALOG:
            	  AlertDialog.Builder builder = new AlertDialog.Builder(this);
            	  builder.setMessage("No se permite Codigo o Pais vacio")
            	           
            	  .setCancelable(false)
                  .setNeutralButton("OK", new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int id) {
                          try {
							this.finalize();
						} catch (Throwable e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
                      }
                  });

            	        
            	 AlertDialog alert = builder.create();
            	 return alert;
            	  
              }
              return null;
          }
	public boolean onCreateOptionsMenu(Menu menu) {
	    	boolean result = super.onCreateOptionsMenu(menu);
	        menu.add(0, INSERT_ID, 0, R.string.Add);
	        return result;
	    }
	    public boolean onOptionsItemSelected(MenuItem item) {
	    
	    switch (item.getItemId()) {
	    case INSERT_ID:
	     String codigo =code.getText().toString();
	     String pais =country.getText().toString();

	     if(codigo.isEmpty() || pais.isEmpty()){
	    	 
	    	 showDialog(ERROR_DIALOG);
	    	 return true;
	     }
	    	Intent intent = new Intent(this,PracticaActivity.class); 
	        Bundle bundle = new Bundle(10);
	        
	        bundle.putString("code", code.getText().toString());
	        
	        bundle.putString("country", country.getText().toString());
	        bundle.putString("fecha",mDateDisplay.getText().toString() );
	        intent.putExtras(bundle);
	        
	        Log.v("Pantalla_add", "reinicia pantalla");
	       setResult(Activity.RESULT_OK, intent); 
	      finish();
	       
	        return true;
	    }
	    return super.onOptionsItemSelected(item);
	    }
}

